from exts import db
# from datetime import datetime
from flask_login import UserMixin
from exts import login_manager
from datetime import datetime

# class UserModel(db.Model):
#     __tablename__ = "user"
#     id = db.Column(db.Integer, primary_key = True, autoincrement=True)
#     username = db.Column(db.String(100), nullable=False)
#     password = db.Column(db.String(100), nullable=False)
#     email = db.Column(db.String(100), nullable=False, unique=True)
#     join_time = db.Column(db.DateTime, default=datetime.now)

class Admin(UserMixin, db.Model):
    __tablename__ = 'admin'
    admin_id = db.Column(db.String(6), primary_key=True)
    admin_name = db.Column(db.String(32))
    password = db.Column(db.String(24))
    right = db.Column(db.String(32))

    def __init__(self, admin_id, admin_name, password, right):
        self.admin_id = admin_id
        self.admin_name = admin_name
        self.password = password
        self.right = right

    def get_id(self):
        return self.admin_id

    def verify_password(self, password):
        if password == self.password:
            return True
        else:
            return False

    def __repr__(self):
        return '<Admin %r>' % self.admin_name


class Book(db.Model):
    __tablename__ = 'book'
    isbn = db.Column(db.String(13), primary_key=True)
    book_name = db.Column(db.String(64))
    author = db.Column(db.String(64))
    press = db.Column(db.String(32))
    class_name = db.Column(db.String(64))
    descn = db.Column(db.String(200))

    def __repr__(self):
        return '<Book %r>' % self.book_name


class Student(UserMixin, db.Model):
    __tablename__ = 'student'
    card_id = db.Column(db.String(8), primary_key=True)
    student_id = db.Column(db.String(9))
    student_name = db.Column(db.String(32))
    sex = db.Column(db.String(2))
    telephone = db.Column(db.String(11), nullable=True)
    enroll_date = db.Column(db.String(13))
    valid_date = db.Column(db.String(13))
    loss = db.Column(db.Boolean, default=False)  # 是否挂失
    debt = db.Column(db.Boolean, default=False)  # 是否欠费
    password = db.Column(db.String(30))


    def __init__(self, card_id, student_id, student_name, sex, telephone, enroll_date, valid_date, loss, debt, password):
        self.card_id = card_id
        self.student_id = student_id
        self.student_name = student_name
        self.sex = sex
        self.telephone = telephone
        self.enroll_date = enroll_date
        self.valid_date = valid_date
        self.loss = loss
        self.debt = debt
        self.password = password

    def get_id(self):
        return self.student_id

    def verify_password(self, password):
        if password == self.password:
            return True
        else:
            return False

    def __repr__(self):
        return '<Student %r>' % self.student_name


class Inventory(db.Model):
    __tablename__ = 'inventory'
    barcode = db.Column(db.String(6), primary_key=True)
    isbn = db.Column(db.ForeignKey('book.isbn'))
    storage_date = db.Column(db.String(13))
    location = db.Column(db.String(32))
    withdraw = db.Column(db.Boolean, default=False)  # 是否注销
    status = db.Column(db.Boolean, default=True)  # 是否在馆
    admin = db.Column(db.ForeignKey('admin.admin_id'))  # 入库操作员

    def __repr__(self):
        return '<Inventory %r>' % self.barcode


class ReadBook(db.Model):
    __tablename__ = 'readbook'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    barcode = db.Column(db.ForeignKey('inventory.barcode'), index=True)
    card_id = db.Column(db.ForeignKey('student.card_id'), index=True)
    start_date = db.Column(db.String(13))
    borrow_admin = db.Column(db.ForeignKey('admin.admin_id'))  # 借书操作员
    end_date = db.Column(db.String(13), nullable=True)
    return_admin = db.Column(db.ForeignKey('admin.admin_id'))  # 还书操作员
    due_date = db.Column(db.String(13))  # 应还日期

    def __repr__(self):
        return '<ReadBook %r>' % self.id


@login_manager.user_loader
def load_user(admin_id):
    result = Student.query.filter_by(student_id=admin_id).first()
    if result is None:
        result = Admin.query.get(int(admin_id))
    print(result)
    return result




#Yigong's Model
#689集成为一个图书社区
#帖子、回复、公告
class PostModel(db.Model):
    __tablename__ = 'post'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    title = db.Column(db.String(100),nullable=False)
    content = db.Column(db.Text,nullable=False)
    image_url = db.Column(db.String(200),nullable=True)
    create_time = db.Column(db.DateTime,default=datetime.now)
    type = db.Column(db.Integer,nullable=False)
    likes = db.Column(db.Integer,default=0)
    #外键
    author_id = db.Column(db.String(8), db.ForeignKey('student.card_id'))

    #反向引用
    #　author = db.relationship(Student,backref=db.backref('posts'))

class CommentsModel(db.Model):
    __tablename__ = 'comment'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    content = db.Column(db.Text,nullable=False)
    create_time = db.Column(db.DateTime,default=datetime.now)
    image_url = db.Column(db.String(200),nullable=True)

    #回复针对哪个问题以及是有谁发布，需要两个外键
    author_id = db.Column(db.String(8),db.ForeignKey('student.card_id'))
    post_id = db.Column(db.Integer,db.ForeignKey('post.id'))

    #为了操作需要，添加两个关系，用question拿到所有anwser,用author拿到所有answer
    #使用创建时间来排序
    post = db.relationship(PostModel,backref=db.backref('comments',order_by=create_time.desc()))


class AnnounceModel(db.Model):
    __tablename__ = 'announce'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    title = db.Column(db.String(100),nullable=False)
    content = db.Column(db.Text,nullable=False)
    image_url = db.Column(db.String(200),nullable=True)
    create_time = db.Column(db.DateTime,default=datetime.now)

    #外键
    author_id = db.Column(db.String(6),db.ForeignKey('admin.admin_id'))
    #反向引用
    # author = db.relationship(Admin,backref=db.backref('announces'))

